// 二分法 时间复杂度 O(log n) 空间复杂度 O(1)

class Solution {
public:
    vector<int> searchRange(vector<int>& nums, int target) {
        int n = nums.size();
        vector<int> ret = {-1, -1};
        if (n == 0) return ret;
        int left = 0, right = n - 1, mid;
        while (left <= right) {
            mid = left + (right - left) / 2;
            if (nums[mid] < target) {
                left = mid + 1;
            }
            else if (nums[mid] > target) {
                right = mid - 1;
            }
            else {
                ret[0] = mid;
                right = mid - 1;
            }
        }
        if (ret[0] == -1) return ret;
        right = n - 1;
        while (left <= right) {
            mid = left + (right - left) / 2;
            if (nums[mid] < target) {
                left = mid + 1;
            }
            else if (nums[mid] > target) {
                right = mid - 1;
            }
            else {
                ret[1] = mid;
                left = mid + 1;
            }
        }
        return ret;
    }
};